package ar.edu.utn.frba.dds.opendesigners.moke;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import ar.edu.utn.frba.dds.opendesigners.aerolineas.InterfaceLanchita;

import com.lanchita.excepciones.CodigoErroneoException;
import com.lanchita.excepciones.EstadoErroneoException;


public class LanchitaMoke implements InterfaceLanchita {


	public String[][] asientos;

	// ****************************************************************
	// ** CONSTRUCTORS
	// ****************************************************************

	public LanchitaMoke() {
		this.setAsientos(new String[][] {
				{ "01202022220202-3", "159.90", "P", "V", "D", "", "14:00","02:25","EZE","USA","20/12/2012","21/12/2012" },
				{ "01202022220123-3", "205.10", "E", "P", "D", "", "14:00","02:25","EZE","USA","20/12/2012","21/12/2012" },
				{ "01202012315523-8", "154.08", "E", "P", "D", "", "14:00","02:25","EZE","USA","20/12/2012","21/12/2012" },
				{ "01202022267867-7", "255.98", "E", "P", "D", "", "05:20","14:00","EZE","PER","20/12/2012","20/12/2012" },
				{ "01202022227897-3", "236.10", "P", "C", "D", "", "05:20","14:00","EZE","PER","20/12/2012","20/12/2012" },
				{ "01202022998988-6", "148.23", "P", "V", "D", "", "05:20","14:00","EZE","PER","20/12/2012","20/12/2012" },
				{ "01202022220008-3", "383.22", "T", "V", "D", "", "20:00","08:00","PER","USA","20/12/2012","21/12/2012" },
				{ "01202022256565-3", "282.19", "T", "C", "D", "", "20:00","08:00","PER","USA","20/12/2012","21/12/2012" },
				{ "01202022323423-5", "431.28", "T", "C", "D", "", "20:00","08:00","PER","USA","20/12/2012","21/12/2012" },
				{ "01202022220298-2", "528.81", "P", "V", "D", "", "07:00","08:00","AEO","USH","20/12/2012","21/12/2012"} });
	}


	/* (non-Javadoc)
	 * @see ar.edu.utn.frba.dds.opendesigners.moke.InterfaceLanchita#asientosDisponibles(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public String[][] asientosDisponibles(String origen, String destino,
			String fechaSalida, String horaSalida, String fechaLlegada,
			String horaLlegada) {

		List<String[]> disponibles = Arrays.asList(this.getAsientos());
		List<String[]> retVal = new ArrayList<String[]>();
		Iterator<String[]> iterator = disponibles.iterator();

		while (iterator.hasNext()) {
			String[] element = iterator.next();
			if (element[4].equals("D")) {
				retVal.add(element);
			}
		}

		return retVal.toArray(new String[][] {});
	}

	/* (non-Javadoc)
	 * @see ar.edu.utn.frba.dds.opendesigners.moke.InterfaceLanchita#getIndiceDeAsiento(java.lang.String)
	 */
	public int getIndiceDeAsiento(String codigo) {
		for (int i = 0; i < this.getAsientos().length; i++) {
			if (this.getAsientos()[i][0].equals(codigo)) {
				return i;
			}
		}

		throw new CodigoErroneoException(codigo);
	}


	/* (non-Javadoc)
	 * @see ar.edu.utn.frba.dds.opendesigners.moke.InterfaceLanchita#comprar(java.lang.String)
	 */
	public void comprar(String codigo) {
		String[] asiento = this.getAsientos()[this.getIndiceDeAsiento(codigo)];

		if (asiento[4].equals("C") || asiento[4].equals("R")) {
			throw new EstadoErroneoException();
		}

		asiento[4] = "C";
	}


	/* (non-Javadoc)
	 * @see ar.edu.utn.frba.dds.opendesigners.moke.InterfaceLanchita#reservar(java.lang.String, java.lang.String)
	 */
	public void reservar(String codigo, String dni) {
		String[] asiento = this.getAsientos()[this.getIndiceDeAsiento(codigo)];

		if (!asiento[4].equals("D")) {
			throw new EstadoErroneoException();
		}

		asiento[4] = "R";
	}

	/* (non-Javadoc)
	 * @see ar.edu.utn.frba.dds.opendesigners.moke.InterfaceLanchita#getAsientos()
	 */
	public String[][] getAsientos() {
		return this.asientos;
	}

	/* (non-Javadoc)
	 * @see ar.edu.utn.frba.dds.opendesigners.moke.InterfaceLanchita#setAsientos(java.lang.String[][])
	 */
	public void setAsientos(String[][] asientos) {
		this.asientos = asientos;
	}
	
	
	
}
